using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Domain.ActiveDirectory.Model;
using Domain.ActiveDirectory.Provider;
using Domain.Provider;
using WebUI.Models;

namespace WebUI.Controllers
{
    public class ManagerADGroupController : Controller
    {
        private IActiveDirectoryDataProvider _adProvider;

        public ManagerADGroupController(IActiveDirectoryDataProvider adProvider)
        {
            _adProvider = adProvider;
        }

        public ActionResult Edit(string groupName)
        {
            var group = _adProvider.GetGroupsByName(groupName).FirstOrDefault();
            var model = new GroupViewModel(group);
            return View(model);
        }

        public ActionResult AddMember(string groupName)
        {
            MemberViewModel model = new MemberViewModel();
            model.GroupName = groupName;
            IEnumerable<User> managers = _adProvider.GetUsersByTitle("General Manager", "Assistant Manager", "IT EP Developer").OrderBy(x => x.CompleteName);
            ViewBag.Managers = managers;
            return View(model);
        }

        public ActionResult MoveMember(string groupName)
        {
            MoveMemberViewModel model = new MoveMemberViewModel() 
            { 
                FromGroupName = groupName
            };
            ViewBag.ManagerGroups = _adProvider.GetGroupsByName("*GM", "AM*");
            ViewBag.Members = _adProvider.GetGroupsByName(groupName).FirstOrDefault().MemberNames;
            return View(model);
        }

        [HttpPost]
        public ActionResult MoveMember(MoveMemberViewModel model)
        {
            Group fromGroup = _adProvider.GetGroupsByName(model.FromGroupName).FirstOrDefault();
            Group toGroup = _adProvider.GetGroupsByName(model.ToGroupName).FirstOrDefault();
            User memberToMove = _adProvider.GetUsersByCompleteName(model.MemberName).FirstOrDefault();
            _adProvider.MoveUserFromGroupToGroup(fromGroup, toGroup, memberToMove);
            return RedirectToAction("Edit", new { groupName = model.ToGroupName });
        }

        [HttpPost]
        public ActionResult Save(MemberViewModel model)
        {
            Group group = _adProvider.GetGroupsByName(model.GroupName).FirstOrDefault();
            User user = _adProvider.GetUsersByCompleteName(model.Name).FirstOrDefault();
            _adProvider.AddUserToGroup(group, user);
            return RedirectToAction("Edit", new { groupName = model.GroupName });
        }

        public ActionResult DeleteMember(MemberViewModel model)
        {
            Group group = _adProvider.GetGroupsByName(model.GroupName).FirstOrDefault();
            User userToDeleteFromGroup = _adProvider.GetUsersByCompleteName(model.Name).FirstOrDefault();
            _adProvider.RemoveUserFromGroup(group, userToDeleteFromGroup);
            return RedirectToAction("Edit", new { groupName = model.GroupName });
        }

        public ActionResult Index()
        {
            List<GroupViewModel> managerGroups = new List<GroupViewModel>();
            foreach (Group g in _adProvider.GetGroupsByName("*GM", "AM*"))
            {
                managerGroups.Add(new GroupViewModel(g));
            }
            return View(managerGroups);	
        }

        public ActionResult ManagerGroupsGridView()
        {
            List<GroupViewModel> managerGroups = new List<GroupViewModel>();
            foreach (Group g in _adProvider.GetGroupsByName("*GM", "AM*"))
            {
                managerGroups.Add(new GroupViewModel(g));
            }
            return PartialView("_ManagerGroupsGridView", managerGroups);
        }

        //public ActionResult MembersGridView(string groupName)
        //{
        //    return PartialView("_MembersGridView", _adProvider.GetGroupsByName(groupName));
        //}
	}
}